/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name:type.c
> Author:xxx
> Mail: 
> Created Time: 2024年12月04日 星期三 16时08分15秒
***************************************************/

#include "cJSON.h"
#include "style.h"

int cjson_type(char *str){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *type = cJSON_GetObjectItem(root,"type");
	
	//获取字符串类型的值
	char *type_1 = cJSON_GetStringValue(type);
	printf("%s\n",type_1);
	if(strcmp(type_1,"gly_login") == 0){			//管理员登陆
		return 1;
	}else if(strcmp(type_1,"staff_login") == 0){	//员工登陆
		return 2;
	}else if(strcmp(type_1,"kin_login") == 0){		//家属登陆
		return 3;
	}else if(strcmp(type_1,"kin_register") == 0){	//家属注册
		return 4;
	}else if(strcmp(type_1,"staff_register") == 0){	//员工注册
		return 5;
	}else if(strcmp(type_1,"delete_nurse") == 0){	//删除员工
		return 6;
	}else if(strcmp(type_1,"delete_kin") == 0){		//删除家属
		return 7;
	}else if(strcmp(type_1,"staff_change_passwd") == 0){//员工修改密码
		return 8;
	}else if(strcmp(type_1,"kin_change_passwd") == 0){	//家属修改密码
		return 9;
	}else if(strcmp(type_1,"staff_leave") == 0){	//员工请假
		return 10;
	}
	return 0;
}

//判断管理员是否登陆成功
int gly_login(int client_socket,char *str){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *passwd = cJSON_GetObjectItem(root,"passwd");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *passwd_1 = cJSON_GetStringValue(passwd);
	
	if(strcmp(username_1,"gly") == 0 && strcmp(passwd_1,"123456") == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断员工、家属是否登陆成功
int staff_kin_login(int client_socket,MYSQL *mysql,char *str,int num){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *passwd = cJSON_GetObjectItem(root,"passwd");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *passwd_1 = cJSON_GetStringValue(passwd);
	char src[1024] = {0};
	if(num == 2){
		//判断员工是否登陆成功
		sprintf(src,"select * from nurse where nursename = %s and passwd = %s",username_1,passwd_1);
	}else{
		//判断家属是否登陆成功
		sprintf(src,"select * from family_members where username = %s and passwd = %s",username_1,passwd_1);
	}
	
	if(mysql_query(mysql,src) != 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断员工、家属是否注册成功
int staff_kin_register(int client_socket,MYSQL *mysql,char *str,int num){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *passwd = cJSON_GetObjectItem(root,"passwd");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *passwd_1 = cJSON_GetStringValue(passwd);
	char src[1024] = {0};
	printf("%d\n",num);
	if(num == 4){
		//判断家属是否注册成功
		sprintf(src,"insert into family_members(username,passwd) values('%s','%s')",username_1,passwd_1);
	}else{
		//判断员工是否注册成功
		sprintf(src,"insert into nurse(nursename,passwd) values('%s','%s')",username_1,passwd_1);
	}
	printf("%s\n",src);
	if(mysql_query(mysql,src) == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断删除员工、家属是否成功
int delete_0(int client_socket,MYSQL *mysql,char *str,int num){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	
	char *username_1 = cJSON_GetStringValue(username);
	char src[1024] = {0};
	if(num == 6){
		//判断删除员工是否成功
		sprintf(src,"delete from nurse where nursename='%s'",username_1);
	}else{
		//判断删除家属是否成功
		sprintf(src,"delete from family_members where username='%s'",username_1);
	}
	printf("%s\n",src);
	if(mysql_query(mysql,src) != 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断员工、家属修改密码是否成功
int change_passwd(int client_socket,MYSQL *mysql,char *str,int num){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *passwd = cJSON_GetObjectItem(root,"passwd");
	cJSON *newpasswd = cJSON_GetObjectItem(root,"newpasswd");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *passwd_1 = cJSON_GetStringValue(passwd);
	char *newpasswd_1 = cJSON_GetStringValue(newpasswd);
	char src[1024] = {0};
	if(num == 8){
		sprintf(src,"select * from nurse where nursename = %s and passwd = %s",username_1,passwd_1);
	}else{
		sprintf(src,"select * from family_members where username = %s and passwd = %s",username_1,passwd_1);
	}
	if(mysql_query(mysql,src) != 0){
		memset(src,0,1024);
		if(num == 8){
			//员工修改密码
			sprintf(src,"update nurse set passwd='%s' where nursename = '%s'",newpasswd_1,username_1);
		}else{
			//用户修改密码
			sprintf(src,"update family_members set passwd='%s' where username = '%s'",newpasswd_1,username_1);
		}
	}
	if(mysql_query(mysql,src) == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断员工请假申请是否提交成功
int leave(int client_socket,MYSQL *mysql,char *str){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *reason = cJSON_GetObjectItem(root,"reason");
	cJSON *start_time = cJSON_GetObjectItem(root,"start_time");
	cJSON *end_time = cJSON_GetObjectItem(root,"end_time");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *reason_1 = cJSON_GetStringValue(reason);
	char *start_time_1 = cJSON_GetStringValue(start_time);
	char *end_time_1 = cJSON_GetStringValue(end_time);
	
	char src[1024] = {0};
	sprintf(src,"insert into leaves(nursename,reason,start_time,end_time) values('%s','%s','%s','%s')",\
	username_1,reason_1,start_time_1,end_time_1);
	printf("%s\n",src);
	if(mysql_query(mysql,src) == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

























